

global filename jmp_welfare_targeting_table

//first generate ratios for the adjusted temperature welfare data


use "stata data/regsave/FE_new_all_inland_temp_f_interact", clear
keep if inlist(var,"pdp","t_pdp")
duplicates drop

keep var coef

gen temp_interaction = 0
replace temp_interaction = 1 if var == "t_pdp"

drop var

gen id = 1

reshape wide coef, i(id) j(temp)

rename coef0 coef_constant
rename coef1 coef_temp

save "stata data/temp_coefs", replace


use "stata data/main regs dataset", clear
keep if coastal == 0

gen id = 1

capture drop _merge
merge m:1 id using "stata data/temp_coefs"
drop _merge

keep if year == 2015
order date sa kwh temp_f coef*
gen reduction = coef_constant + coef_temp*temp_f*kwh_collapse
order date sa kwh temp_f coef* reduction
collapse (sum) reduction, by(date)
egen average_reduction = mean(reduction)
gen ratio = reduction / average_reduction
keep date ratio
save "stata data/reductions ratio", replace



use "stata data/max load for welfare calculations mw_reductions_fe_100_101 price_adder_6.dta", clear
gsort -max_load
keep if _n <=20


keep if event_day == 1

merge 1:1 date using "stata data/reductions ratio"
drop _merge


merge 1:1 date using "raw data/trigger and forcasted temps"
keep if _merge == 3



keep date event_day max_load ratio forecast_temps pdptriggertemperature total_CS_losses mw_reductions *sim*

capture gen per_mw_construction_cost = 1159000 //from page 137 of cec report
gen total_construction_costs = mw_reductions_sim* per_mw_construction 

local total_construction_cost = total_construction_costs
payper,pv(`total_construction_cost') nper(30) freq(y) rate(.03)
gen construction_30_year = `r(PMT)'


gen yearly_om_costs = 25.95 * mw_reductions_sim*1000

gen capacity_savings_per_year = yearly_om_costs + construction_30_year

//figure out MW reductions -- 


//consumer surplus losses
gen double CS_cost =  round(total_CS_losses_sim) //also make this the sim thing
gen temp= CS_cost // * ratio //turn off the ratio thing
gen double CS_adjusted = round(temp,1000)
drop temp
format CS_adjusted %10.0fc





//capacity value
gsort -max


egen new_peak = max(max_load - mw_reductions_sim)
gen double percent_reduction_on_2nd_day = (max_load - new_peak)/mw_reductions_sim
replace percent_reduction_on_2nd_day  = 0 if percent_reduction_on_2nd_day  <0

//this makes it so the first day doesn't get all the benefits
replace percent_reduction_on_2nd_day  = percent_reduction_on_2nd_day[_n] - percent_reduction_on_2nd_day[_n+1] 

gen capacity_value = percent_reduction_on_2nd_day  * capacity_savings_per_year


//this is to drop the total capacity value for the first day by the 2nd day amount.
//This indicates that you only get the first 8.4 million on the first day
//and the subseqnet 1.6 million for the second



format capacity_value %15.1fc
replace capacity_value = round(capacity_value,100000)

replace capacity_value = . if capacity_value == 0



//a quick explination
/*
R1 wanted this table to reflect the different temperatures. 
So I re-estimated things based on the trigger temps and got different coefficents for <100, 101-102, 103-104.
I decided not to apply those for the whole calculations, but just apply them to this table since on average they are right...
*/
//somewhat awkwardway to bring in the right values for cs_adjusted
preserve
use "stata data/max load for welfare calculations mw_reductions_fe_102_104 price_adder_6", clear
gen double CS_cost_adjusted_103_104 =  round(total_CS_losses_sim,1000) //also make this the sim thing
keep CS_cost_adjusted_103_104 
duplicates drop
sum CS_cost_adjusted_103_104 
global CS_cost_adjusted_103_104 = `r(mean)'
restore

replace CS_adjusted = $CS_cost_adjusted_103_104 if inlist(forecast_temps,103,104)
replace CS_adjusted = 0 if forecast_temps<100




capture file close table1		
file open table1 using "latex/$filename.tex", write text replace


file write table1 "\label{$filename}" _n
file write table1 "\label{ttest table}" _n
file write table1 "\vspace{-2mm}" _n
file write table1 "\footnotesize" _n
file write table1 "\begin{tabular}{@{\extracolsep{4pt}}lccc@{}ccccc}" _n //this is where you change justification. Options are l c r
*extracolsep from http://tex.stackexchange.com/questions/101000/how-to-shorten-cline-so-that-multiple-groups-in-a-table-become-visible

file write table1 "\hline\hline \\ [-.9em]" _n 

*file write table1 "\multicolumn{1}{c}{$\begin{matrix}\text{Baseline}\\ \text{Territory} \end{matrix}$}& 	\multicolumn{1}{c}{$\begin{matrix}\text{Average}\\ \text{Temperature} \end{matrix}$} &	\multicolumn{1}{c}{$\begin{matrix}\text{Firm}\\ \text{Count} \end{matrix}$}	&	\\" _n
*file write table1 " Energy use classification && Median & & &Thirds	\\" _n 
*file write table1 "  \cline{2-4} \cline{5-7} \\" _n 
*file write table1 "[0.2em]" _n


local col1_top "Event day"
local col1_bottom  ""
local col2_top "PG\&E"
local col2_bottom "max load"

local col3_top "Annual capacity"
local col3_middle "cost savings"
local col3_bottom "(discounted)"



*local col3_bottom "\$C(X_{t})-C(X^{*}_{t})\$"
*local col3_bottom "\$ C(X_{t})- C(X^{*}_{t})$"




local col4_top "Annual net consumer"
local col4_middle "and utility surplus loss"
local col4_bottom "(discounted)"




local col6_top "NWS day-ahead"
local col6_middle "max temperature"
local col6_bottom "forecast"


local col7_top "Trigger"
local col7_bottom "temperature"



local col1 "\multicolumn{1}{l}{$\begin{matrix}\text{`col1_top'}\\ \text{`col1_bottom'} \end{matrix}$}"
local col2 "\multicolumn{1}{c}{$\begin{matrix}\text{`col2_top'}\\ \text{`col2_bottom'} \end{matrix}$}"
local col3 "\multicolumn{1}{c}{$\begin{matrix}\text{`col3_top'}\\ \text{`col3_middle'}\\ \text{`col3_bottom'} \end{matrix}$}"
local col4 "\multicolumn{1}{c}{$\begin{matrix}\text{`col4_top'}\\ \text{`col4_middle'}\\ \text{`col4_bottom'} \end{matrix}$}"
local col5 "\multicolumn{1}{c}{$\begin{matrix}\text{`col5_top'}\\ \text{`col5_bottom'} \end{matrix}$}"
local col6 "\multicolumn{1}{c}{$\begin{matrix}\text{`col6_top'}\\ \text{`col6_middle'}\\ \text{`col6_bottom'} \end{matrix}$}"
local col7 "\multicolumn{1}{c}{$\begin{matrix}\text{`col7_top'}\\ \text{`col7_bottom'} \end{matrix}$}"

file write table1 " `col1_top' & `col2' & `col3' & `col4'  & `col6' & `col7'	\\" _n


local math1 "Model input"
local math2 "\$ L^{*}_{t}$"
local math3 "\$ C(X_{t})- C(X^{*}_{t})$"
local math4 "\$ CS_{t}$"
local math5 "\$ S_{t}$"
local math6

*file write table1 "[0.6cm]" _n
*file write table1 " `math1' & `math2' & `math3' & `math4' 	\\" _n




*file write table1 "  Event Date & All PG\&E average temperature& Coastal firm average temperatures & Inland firm average temperatures \\" _n
file write table1 "[0.1em]" _n
file write table1 "\hline \\" _n
file write table1 "[-0.7em]" _n




capture gen row = _n


gen year = year(date)
gen month = month(date)
gen day = day(date)

tostring year month day, replace

gen date_str = month + "/" + day + "/" + year

gen id = _n



levelsof id, local(vars)
foreach v of local vars  {
  qui sum row if id==`v'
  local r = r(mean)
  
  	
	local r = r(mean)
	local t1 = date_str[`r']
	local t2 = string(max_load[`r'], "%9.0fc")
if `r' <= 2 {
	local t3 = "\\$" + string(capacity_value[`r'],"%12.0fc") 
	}
else {	
local t3 =  "\\$0"
}
	local t4 = "-\" + "\\$" + string(CS_adjusted[`r'],"%9.0fc")
	


	local t6 = forecast_temps[`r']
	local t7 = pdptriggertemperature[`r']
	
*local style "\textbf{"	
local style "{"	
		
	if `r' <= 2 {
	file write table1 "`style'`t1'} & `style'`t2'} & `style'`t3'} & `style'`t4'} &  `style'`t6'} & `style'`t7'}   \\" _n
	file write table1 "[0.2em] " _n
	}
	else{
	file write table1 "`t1' & `t2' & `t3' & `t4' & `t6' & `t7'   \\" _n
	file write table1 "[0.2em] " _n
	}
}
*


	




file write table1 "\hline" _n
file write table1 "\end{tabular}" _n

file close table1		




